package com.hy.frame.recycler;

import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public abstract class BaseItemDecoration extends RecyclerView.ItemDecoration {
    protected RecyclerView rcyList;
    protected int divider;
    protected int dividerVertical;
    protected Drawable drawable;
    protected Paint mPaint;
    protected int paddingTop;
    protected int paddingLeft;
    protected int paddingRight;
    protected int paddingBottom;
    protected BaseAdapter<?> adapter;

    public BaseItemDecoration(RecyclerView rcyList, int divider) {
        super();
        this.rcyList = rcyList;
        this.divider = divider;
    }

    public BaseItemDecoration(RecyclerView rcyList, int divider, int color) {
        this(rcyList, divider);
        this.mPaint = new Paint();
        this.mPaint.setColor(color);
        this.mPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
    }

    public BaseItemDecoration(RecyclerView rcyList, int divider, Drawable drawable) {
        this(rcyList, divider);
        this.drawable = drawable;
    }


    public BaseItemDecoration setDividerVertical(int dividerVertical) {
        this.dividerVertical = dividerVertical;
        return this;
    }


    public BaseItemDecoration setPaddingTop(int paddingTop) {
        this.paddingTop = paddingTop;
        return this;
    }

    public BaseItemDecoration setPaddingLeft(int paddingLeft) {
        this.paddingLeft = paddingLeft;
        return this;
    }

    public BaseItemDecoration setPaddingRight(int paddingRight) {
        this.paddingRight = paddingRight;
        return this;
    }

    public BaseItemDecoration setPaddingBottom(int paddingBottom) {
        this.paddingBottom = paddingBottom;
        return this;
    }

    public BaseItemDecoration build() {
        return this;
    }

    /**
     * onDraw是在itemview绘制之前
     *
     * @param c      Canvas
     * @param parent RecyclerView
     * @param state  RecyclerView.State
     */
    @Override
    public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        //super.onDraw(c, parent, state);
        if (this.mPaint != null && this.mPaint.getAlpha() == 0) { //未定义 Paint
            return;
        }
        //Android 4.4
        if (drawable != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT && drawable.getAlpha() == 0) {
            return;
        }
        drawDivider(c);
    }

    /**
     * onDrawOver是在itemview绘制之后
     *
     * @param c      Canvas
     * @param parent RecyclerView
     * @param state  RecyclerView.State
     */
    @Override
    public void onDrawOver(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        super.onDrawOver(c, parent, state);
    }

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
        //super.getItemOffsets(outRect, view, parent, state);
        if (adapter == null) {
            if (parent.getAdapter() == null || parent.getAdapter() instanceof BaseAdapter<?>)
                throw new RuntimeException("adapter is not BaseAdapter");
            adapter = (BaseAdapter<?>) parent.getAdapter();
        }
        configureItemOutRect(outRect, view);
    }

    abstract void drawDivider(Canvas canvas);


    abstract void configureItemOutRect(Rect outRect, View view);
}
